package response

import (
	"time"

	"github.com/gin-gonic/gin"
)

// Response 定义全局接口返回数据结构
type Response struct {
	Code      int         `json:"code"`       // 状态码
	Message   string      `json:"message"`    // 消息
	Data      interface{} `json:"data"`       // 数据
	Timestamp int64       `json:"timestamp"`  // 时间戳
	RequestID string      `json:"request_id"` // 请求 ID
}

// Success 返回成功响应
func Success(c *gin.Context, data interface{}) {
	resp := Response{
		Code:      200,
		Message:   "success",
		Data:      data,
		Timestamp: time.Now().UnixMilli(),
		RequestID: c.GetString("X-Request-ID"),
	}
	if resp.RequestID == "" {
		resp.RequestID = c.Request.Header.Get("X-Request-ID")
	}
	c.JSON(200, resp)
}

// Fail 返回失败响应
func Fail(c *gin.Context, code int, message string) {
	resp := Response{
		Code:      code,
		Message:   message,
		Data:      nil,
		Timestamp: time.Now().UnixMilli(),
		RequestID: c.GetString("X-Request-ID"),
	}
	if resp.RequestID == "" {
		resp.RequestID = c.Request.Header.Get("X-Request-ID")
	}
	c.JSON(200, resp)
}
